//
//  BLGetDouyuData.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/6/1.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit

class BLGetDouyuData: NSObject {
    
    // 单例对象
    static let shareInstance: BLGetDouyuData = BLGetDouyuData()
    
    
    //douyu API BeseUrl
    private let baseUrlStr = "http://capi.douyucdn.cn/api/v1/"
    //直播rmbp BeseUrl
    private let liveBaseUrl : String = "http://douyutv.com/api/v1/"
    //拼接用的URLStr
    private var roomUrlStr = ""
    //请求参数
    private let baseParameters : [String : AnyObject] = ["aid" : "ios",
                                                         "client_sys" : "ios"]
    //autoKey
    private let authKey = "1231"
    
}


// MARK: - 直播房间内 API
extension BLGetDouyuData{
    /**
     获取礼物数据
     
     - parameter finishedCallback: 数据回调
     */
    
    func getGiftEffectsData(finishedCallback : (dataArray : AnyObject?) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "gift_effects"
        var parameters = baseParameters
        parameters["time"] = Int(NSDate().timeIntervalSince1970)
        
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,
                                        parameters: parameters,
                                        successCallback: { (jsonData) in
                                            //TODO:
                                            bl_writerToDesktop(jsonData, filename: "获取房间礼物")
                                            finishedCallback(dataArray: jsonData)
                                            
        }) { (error) in
            
        }
        
    }
    
    /**
     获取房间广告
     
     - parameter finishedCallback: 数据回调
     */
    func getIndexAdData(finishedCallback : (dataArray : AnyObject?) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "get_index_ad"
        var parameters = baseParameters
        parameters["time"] = Int(NSDate().timeIntervalSince1970)
        
        parameters["cate_id"] = "IOS"
        parameters["place"] = 7
        
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,
                                        parameters: parameters,
                                        successCallback: { (jsonData) in
                                            //TODO:
                                            bl_writerToDesktop(jsonData, filename: "获取房间广告")
                                            finishedCallback(dataArray: jsonData)
                                            
        }) { (error) in
            
        }
    }
    
    
    /**
     获取房间信息
     - parameter roomID:           房间ID
     - parameter finishedCallback: 数据回调
     */
    func getRoomData(roomID roomID : String,
                            finishedCallback : (dataArray : BLRoomItem) -> ()) -> () {
        
        //创建NSURL对象
        //获取时间戳
        let time = Int(NSDate().timeIntervalSince1970)
        //以下是auth参数 算法
        let md5url:String = "room/\(roomID)?aid=android&client_sys=android&time=\(time)"
        let auth : String = md5url + authKey
        
        roomUrlStr = liveBaseUrl + "room/\(roomID)"
        var parameters = [String : AnyObject]()
        parameters["aid"] = "android"
        parameters["client_sys"] = "android"
        
        parameters["time"] = time
        parameters["auth"] = auth.md5
        
        
        
        var dataArray = BLRoomItem()
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            bl_writerToDesktop(jsonData, filename: "获取房间信息")
            
            let jsonData = jsonData as! [String : AnyObject]
            
            //数据校验
            if  jsonData["data"] is String {
                return
            }
            
            let dict = jsonData["data"] as! [String : AnyObject]
            
            dataArray = BLRoomItem.initWithDict(dict)
            
            finishedCallback(dataArray: dataArray)
        }) { (error) in
            
        }
        
    }
}


// MARK: - 无需登录基础 API
extension BLGetDouyuData{
    
    /**
     获取当前全部直播
     - parameter limit:            一次获取的个数
     - parameter offset:           获取偏移位置
     - parameter finishedCallback: 数据回调
     */
    func getAllLiveData(limit limit : Int = 20,
                              offset : Int = 0,
                              finishedCallback : (dataArray : AnyObject?) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "live"
        
        var parameters = baseParameters
        parameters["limit"] = 20
        
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,
                                        parameters: parameters,
                                        successCallback: { (jsonData) in
                                            finishedCallback(dataArray: jsonData)
                                            
        }) { (error) in
            
        }
        
    }
    
    /**
     获取父频道
     - parameter finishedCallback: 数据回调
     */
    func getColumnListData(finishedCallback : (dataArray : [BLColumnListItem]?) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "getColumnList"
        
        let parameters = baseParameters
        
        
        var dataArray = [BLColumnListItem]()
        
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            bl_writerToDesktop(jsonData, filename: "getColumnListData")
            //数据回调
            let jsonData = jsonData as! [String : AnyObject]
            let dictArray = jsonData["data"] as! [[String : AnyObject]]
            //模型转换
            for dict in dictArray{
                let tempItem = BLColumnListItem.initWithDict(dict)
                dataArray.append(tempItem)
            }
            
            finishedCallback(dataArray: dataArray)
            
        }) { (error) in
            
        }
    }
    
    
    /**
     获取子频道
     - parameter shortName: 父频道名字，来自父频道请求返回数据中的short_name
     - parameter finishedCallback: 数据回调
     */
    func getSubDetailData( short_name :String,
                           finishedCallback : (dataArray : [BLDouyuRoomDetalModal]?) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + short_name
        
        let parameters = baseParameters
        
        var dataArray = [BLDouyuRoomDetalModal]()
        
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            //数据回调
            bl_writerToDesktop(jsonData, filename: "紫平刀")
            let jsonData = jsonData as! [String : AnyObject]
            let dictArray = jsonData["data"] as! [[String : AnyObject]]
            //模型转换
            for dict in dictArray{
                let tempItem = BLDouyuRoomDetalModal.initWithDict(dict)
                dataArray.append(tempItem)
            }
            
            finishedCallback(dataArray: dataArray)
            
        }) { (error) in
            
        }
    }
    
    
    /**
     获取父频道所有直播列表
     
     - parameter cate_id:          父频道 cate_id
     - parameter limit:            一次获取的个数
     - parameter offset:           获取偏移位置
     - parameter finishedCallback: 数据回调
     */
    func getColumnLiveData( cate_id : Int,
                            limit : Int = 20,
                            offset : Int = 0,
                            finishedCallback : (dataArray : AnyObject?) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "getColumnRoom/\(cate_id)"
        
        var parameters = baseParameters
        parameters["limit"] = limit
        parameters["offset"] = offset
        
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,
                                        parameters: parameters,
                                        successCallback: { (jsonData) in
                                            finishedCallback(dataArray: jsonData)
                                            
        }) { (error) in
            
        }
    }
    
    /**
     获取子频道所有直播列表
     
     - parameter cate_id:          子频道 tag_id
     - parameter limit:            一次获取的个数
     - parameter offset:           获取偏移位置
     - parameter finishedCallback: 数据回调
     */
    func getSubLiveData( tag_id : NSNumber,
                         limit : Int = 20,
                         offset : Int = 0,
                         finishedCallback : (dataArray : [BLDouyuRoomDetalModal]?) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "live/\(tag_id)"
        
        var parameters = baseParameters
        parameters["limit"] = limit
        parameters["offset"] = offset
        
        var dataArray = [BLDouyuRoomDetalModal]()
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            bl_writerToDesktop(jsonData, filename: "BLDouyuRoomDetalModal")
            
            let jsonData = jsonData as! [String : AnyObject]
            let dictArray = jsonData["data"] as! [[String : AnyObject]]
            //模型转换
            for dict in dictArray{
                let tempItem = BLDouyuRoomDetalModal.initWithDict(dict)
                dataArray.append(tempItem)
            }
            //数据回调
            finishedCallback(dataArray: dataArray)
            
        }) { (error) in
            
        }
    }
    
    
    
    /**
     搜索直播间
     - parameter searchString:     要搜索的内容，房间id或者房间名
     - parameter limit:            一次获取的个数
     - parameter offset:           获取偏移位置
     - parameter finishedCallback: 数据回调
     */
    func getSearchNewData( searchString : String,
                           limit : Int = 20,
                           offset : Int = 0,
                           finishedCallback : (dataModal : BLDouyuSearchModal) -> ()) -> () {
        
        /// http://capi.douyucdn.cn/api/v1/searchNew/%E8%AF%B7/1?limit=20&offset=0
        
        let utf8Str = searchString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
        
        roomUrlStr = baseUrlStr + "searchNew/" + utf8Str + "/1"
        
        var parameters = baseParameters
        
        parameters["limit"] = limit
        parameters["offset"] = offset
        
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            
            //数据回调
            let jsonData = jsonData as? [String : AnyObject]
            
            //数据校验
            if  jsonData!["data"] is String {
                return
            }
            let dictArray = jsonData!["data"] as! [String : AnyObject]
            
            //模型转换
            let dataModal = BLDouyuSearchModal.initWithDict(dictArray)
            
            finishedCallback(dataModal: dataModal)
            
        }) { (error) in
            
        }
    }
}
// MARK: - 快速获得指定数据 API
extension BLGetDouyuData{
    
    /**
     获取 颜值Room 数据
     - parameter finishedCallback: 数据回调
     */
    func getVerticalRoomData(finishedCallback : (dataArray : [BLDouyuRoomDetalModal]) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "getVerticalRoom"
        
        var parameters = baseParameters
        parameters["limit"] = 20
        
        var dataArray = [BLDouyuRoomDetalModal]()
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            bl_writerToDesktop(jsonData, filename: "vertroom")
            
            let jsonData = jsonData as! [String : AnyObject]
            let dictArray = jsonData["data"] as! [[String : AnyObject]]
            //模型转换
            for dict in dictArray{
                let tempItem = BLDouyuRoomDetalModal.initWithDict(dict)
                dataArray.append(tempItem)
            }
            //数据回调
            finishedCallback(dataArray: dataArray)
            
        }) { (error) in
            
        }
        
    }
    
    /**
     获取 直播分类 数据
     
     - parameter finishedCallback: 数据回调
     */
    func getColumnDetailData(finishedCallback : (dataArray : [BLColumnDetailItem]) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "getColumnDetail"
        let parameters = baseParameters
        
        var dataArray = [BLColumnDetailItem]()
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            //数据回调
            let jsonData = jsonData as! [String : AnyObject]
            let dictArray = jsonData["data"] as! [[String : AnyObject]]
            //模型转换
            for dict in dictArray{
                let tempItem = BLColumnDetailItem.initWithDict(dict)
                dataArray.append(tempItem)
            }
            
            finishedCallback(dataArray: dataArray)
            
        }) { (error) in
            
        }
        
    }
    
    /**
     获取 最热room 数据
     
     - parameter finishedCallback: 数据回调
     */
    func getbigDataHotRoomData(finishedCallback : (dataArray : AnyObject?) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "getbigDataHotRoom"
        let parameters = baseParameters
        
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,
                                        parameters: parameters,
                                        successCallback: { (jsonData) in
                                            finishedCallback(dataArray: jsonData)
                                            
        }) { (error) in
            
        }
    }
    
    
    /**
     获取 获取首页分类标题 数据
     
     - parameter finishedCallback: 数据回调
     */
    func getColumnTitleData(finishedCallback : (dataArray : [BLColumnTitleItem]) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "getColumnTitle"
        let parameters = baseParameters
        
        var dataArray = [BLColumnTitleItem]()
        
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            //数据回调
            let jsonData = jsonData as! [String : AnyObject]
            let dictArray = jsonData["data"] as! [[String : AnyObject]]
            //模型转换
            for dict in dictArray{
                let tempItem = BLColumnTitleItem.initWithDict(dict)
                dataArray.append(tempItem)
            }
            
            finishedCallback(dataArray: dataArray)
            
        }) { (error) in
            
        }
        
    }
    
    
    /**
     获取 首页游戏预览 数据
     
     - parameter finishedCallback: 数据回调
     */
    func getCustomRoomData(finishedCallback : (dataArray : [BLLiveRoomItem]) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "getCustomRoom"
        var parameters = baseParameters
        parameters["tagIds"] = "1_2_33_"
        
        var dataArray = [BLLiveRoomItem]()
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            bl_writerToDesktop(jsonData, filename: "BLLiveRoomItem")
            //数据回调
            let jsonData = jsonData as! [String : AnyObject]
            let dictArray = jsonData["data"] as! [[String : AnyObject]]
            //模型转换
            for dict in dictArray{
                let tempItem = BLLiveRoomItem.initWithDict(dict)
                dataArray.append(tempItem)
            }
            
            finishedCallback(dataArray: dataArray)
            
        }) { (error) in
            
        }
    }
    
    
    /**
     获取 推荐页面图片轮播器图片 数据
     
     - parameter finishedCallback: 数据回调
     */
    func getSlideData(finishedCallback : (dataArray : [BLSlideItem]) -> ()) -> () {
        
        roomUrlStr = baseUrlStr + "slide/6"
        let parameters = baseParameters
        
        var dataArray = [BLSlideItem]()
        BLNetWork.shareInstance.HttpGet(urlStr: roomUrlStr,parameters: parameters,successCallback: { (jsonData) in
            bl_writerToDesktop(jsonData, filename: "BLSlideItem")
            //数据回调
            let jsonData = jsonData as! [String : AnyObject]
            let dictArray = jsonData["data"] as! [[String : AnyObject]]
            //模型转换
            for dict in dictArray{
                let tempItem = BLSlideItem.initWithDict(dict)
                dataArray.append(tempItem)
            }
            
            finishedCallback(dataArray: dataArray)
            
        }) { (error) in
            
        }
    }
    
}

